namespace EyeSoft.Data.Nhibernate.Transactional
{
	using System;
	using System.Linq;

	using EyeSoft.Domain;
	using EyeSoft.Domain.Transactional;

	using NHibernate;
	using NHibernate.Linq;

	public class NhibernateReadOnlyTransactionalCollection
		: IReadOnlyTransactionalCollection
	{
		protected readonly ISession session;

		public NhibernateReadOnlyTransactionalCollection(ISession session)
		{
			Enforce.Argument(() => session);

			this.session = session;
		}

		public IQueryable<T> Query<T>() where T : class, IAggregate
		{
			return
				session.Query<T>().Cacheable();
		}

		public T GetByKey<T>(IComparable key) where T : class, IAggregate
		{
			Enforce.Argument(() => key);

			return
				session.Get<T>(key);
		}

		public void Dispose()
		{
			session.Dispose();
		}
	}
}